{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from __future__ import print_function\n",
    "import matplotlib.pyplot as plt\n",
    "from pycocotools.coco import COCO\n",
    "from pycocotools.cocoeval import COCOeval\n",
    "import os, sys, zipfile\n",
    "import urllib.request\n",
    "import shutil\n",
    "import numpy as np\n",
    "import skimage.io as io\n",
    "import pylab\n",
    "pylab.rcParams['figure.figsize'] = (10.0, 8.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "os: nt\n",
      "sys: 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]\n",
      "numpy: 1.13.0, E:\\toolkits.win\\anaconda3-4.4.0\\envs\\dlwin36cocobug\\lib\\site-packages\\numpy\\__init__.py\n"
     ]
    }
   ],
   "source": [
    "# Record package versions for reproducibility\n",
    "print(\"os: %s\" % os.name)\n",
    "print(\"sys: %s\" % sys.version)\n",
    "print(\"numpy: %s, %s\" % (np.__version__, np.__file__))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running demo for *bbox* results.\n"
     ]
    }
   ],
   "source": [
    "annType = ['segm','bbox','keypoints']\n",
    "annType = annType[1]      #specify type here\n",
    "prefix = 'person_keypoints' if annType=='keypoints' else 'instances'\n",
    "print('Running demo for *%s* results.'%(annType))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../../annotations\n",
      "../../annotations/instances_val2014.json\n",
      "../../annotations_trainval2014.zip\n",
      "http://images.cocodataset.org/annotations/annotations_trainval2014.zip\n"
     ]
    }
   ],
   "source": [
    "# Setup data paths\n",
    "dataDir = '../..'\n",
    "dataType = 'val2014'\n",
    "annDir = '{}/annotations'.format(dataDir)\n",
    "annZipFile = '{}/annotations_train{}.zip'.format(dataDir, dataType)\n",
    "annFile = '{}/instances_{}.json'.format(annDir, dataType)\n",
    "annURL = 'http://images.cocodataset.org/annotations/annotations_train{}.zip'.format(dataType)\n",
    "print (annDir)\n",
    "print (annFile)\n",
    "print (annZipFile)\n",
    "print (annURL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Will use annotations in ../../annotations/instances_val2014.json\n"
     ]
    }
   ],
   "source": [
    "# Download data if not available locally\n",
    "if not os.path.exists(annDir):\n",
    "    os.makedirs(annDir)\n",
    "if not os.path.exists(annFile):\n",
    "    if not os.path.exists(annZipFile):\n",
    "        print (\"Downloading zipped annotations to \" + annZipFile + \" ...\")\n",
    "        with urllib.request.urlopen(annURL) as resp, open(annZipFile, 'wb') as out:\n",
    "            shutil.copyfileobj(resp, out)\n",
    "        print (\"... done downloading.\")\n",
    "    print (\"Unzipping \" + annZipFile)\n",
    "    with zipfile.ZipFile(annZipFile,\"r\") as zip_ref:\n",
    "        zip_ref.extractall(dataDir)\n",
    "    print (\"... done unzipping\")\n",
    "print (\"Will use annotations in \" + annFile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading annotations into memory...\n",
      "Done (t=6.03s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    }
   ],
   "source": [
    "#initialize COCO ground truth api\n",
    "cocoGt=COCO(annFile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading and preparing results...\n",
      "DONE (t=0.02s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    }
   ],
   "source": [
    "#initialize COCO detections api\n",
    "resFile='%s/results/%s_%s_fake%s100_results.json'\n",
    "resFile = resFile%(dataDir, prefix, dataType, annType)\n",
    "cocoDt=cocoGt.loadRes(resFile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgIds=sorted(cocoGt.getImgIds())\n",
    "imgIds=imgIds[0:100]\n",
    "imgId = imgIds[np.random.randint(100)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running per image evaluation...\n",
      "Evaluate annotation type *bbox*\n",
      "DONE (t=0.30s).\n",
      "Accumulating evaluation results...\n",
      "DONE (t=0.33s).\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.505\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.697\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.573\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.586\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.519\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.501\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.387\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.594\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.595\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.640\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.566\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.564\n"
     ]
    }
   ],
   "source": [
    "# running evaluation\n",
    "cocoEval = COCOeval(cocoGt,cocoDt,annType)\n",
    "cocoEval.params.imgIds  = imgIds\n",
    "cocoEval.evaluate()\n",
    "cocoEval.accumulate()\n",
    "cocoEval.summarize()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
